package regiongenerator;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.LinkedList;
import java.util.List;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

import regiongenerator.data.Field;
import regiongenerator.data.FieldConstraint;
import regiongenerator.data.Interval;
import regiongenerator.data.Field.Type;

/**
 *
 * @author Daniel Grimm
 */
public class Main {

    static Field[][] region;
    
    int hoveredField = -1;
    
    public static int width = 64;
    public static int height = 64;
    /**
     * @param args the command line arguments
     */
    
    public static void setCam(double x, double y, double z)
    {
    	GL11.glLoadIdentity();
    	//GL11.glRotatef(35.264f, 1.0f, 0.0f, 0.0f);
    	GL11.glRotatef(45f, 1.0f, 0.0f, 0.0f);
    	GL11.glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
    	GL11.glScalef((float)z, (float)z, -(float)z);
    	GL11.glTranslated(x, 0, y);
    }
    
    public static void main(String[] args) {
        region = new Field[width][height];
        
        try {
            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

	// init OpenGL
	GL11.glMatrixMode(GL11.GL_PROJECTION);
	GL11.glLoadIdentity();
	GL11.glOrtho(-100.0f, 100.0f, -100.0f, 100.0f, -500.0f, 500.0f);
	GL11.glMatrixMode(GL11.GL_MODELVIEW);
	setCam(0, 0, 0);
	
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
        	region[x][y] = new Field((int) (Math.random()*10));
        	if (Math.random() > 0.5)
        		region[x][y].setType(Type.ROCK);
        	else
        		region[x][y].setType(Type.SOIL);
        }
    }
    
    double camx = 0, camy = 0, camz = 1;
	
    
	while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
	    // Clear the screen and depth buffer
	    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
	    
	    	drawRegion();
	    	
            if (Keyboard.isKeyDown(Keyboard.KEY_UP))
            {
            	camx++;
            	camy--;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
            {
            	camx--;
            	camy++;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
            {
            	camx++;
            	camy++;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
            {
            	camx--;
            	camy--;
            }
            
            camz += Mouse.getDWheel()/(500*camz);
            if (camz < 1)
            	camz = 1;
            
            Display.update();
        	setCam(camx, camy, camz);
        }
        Display.destroy();
    }
    
    private static void drawRegion()
    {
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
            	GL11.glTranslated(4, 0, 0);
            	GL11.glLoadName(y*width+x);
            	region[x][y].draw(4);
            }
            GL11.glTranslated(-4*width, 0, -4);
        }
    }
}
